package com.jayson.server;

import com.jayson.server.catalina.Host;
import com.jayson.server.catalina.Mapper;
import com.jayson.server.catalina.Request;
import com.jayson.server.catalina.Response;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.List;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * socket线程
 * 用于监控端口
 */
public class SocketThread extends Thread {

    private int port;

    private ThreadPoolExecutor threadPoolExecutor;

    private List<Host> hostList;

    public SocketThread(int port, ThreadPoolExecutor threadPoolExecutor, List<Host> hostList) {
        this.port = port;
        this.threadPoolExecutor = threadPoolExecutor;
        this.hostList = hostList;
    }



    @Override
    public void run() {
        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(port);
            while (true) {
                Socket socket = serverSocket.accept();
                RequestProcessor requestProcessor = new RequestProcessor(socket, hostList);
                threadPoolExecutor.execute(requestProcessor);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
